package com.example.hospitalmanagement.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.bean.HealthEducationItem

/**
 * 健康宣教中条目所使用的适配器
 */
class HealthEducationActivityAdapter(context: Context) :
    RecyclerView.Adapter<HealthEducationActivityAdapter.EducationHolder>() {
    private val context = context
    lateinit var dataList: MutableList<HealthEducationItem>
    private lateinit var listener: (position: Int) -> Unit
    fun setListener(listener: (position: Int) -> Unit) {
        this.listener = listener
    }

    class EducationHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val educationText: TextView = itemView.findViewById(R.id.education_item_text)
        val educationPicture: ImageView = itemView.findViewById(R.id.education_item_picture)
        val educationDate: TextView = itemView.findViewById(R.id.education_item_date)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EducationHolder {
        val educationHolder = EducationHolder(
            LayoutInflater.from(context).inflate(R.layout.education_items, parent, false)
        )
        educationHolder.itemView.setOnClickListener {
            listener(educationHolder.adapterPosition)
        }
        return educationHolder
    }

    override fun onBindViewHolder(holder: EducationHolder, position: Int) {
        holder.educationText.text = dataList[position].itemText
        holder.educationPicture.setImageResource(dataList[position].itemPicture)
        holder.educationDate.text = dataList[position].itemDate
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}